package com.chen.web;

import com.chen.pojo.Admin;
import com.chen.service.AdminService;
import com.chen.utils.ResponseUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * com.example.demo.controller
 * 作者      : 陈晓
 * 描述      :
 * 创建日期  : 2018/4/27
 * 修改日期  :
 */

@Controller
@RequestMapping("/admin")
public class AdminController {


  @Autowired
  private AdminService adminService;
  private Logger logger = Logger.getLogger(AdminController.class);
  private static final String message = "您输入的用户名和密码有误！！！";

  /**
   * 用户登录验证
   *
   * @param admin
   * @param request
   * @param response
   * @return
   */
  @RequestMapping("/login")
  public String login(Admin admin, HttpServletRequest request, HttpServletResponse response) {
    String returnPage = "index";
    Object loginUser = request.getSession().getAttribute("loginUser");
    if (loginUser == null) {
      Admin login = null;
      try {
        login = adminService.get(admin);
        if (login != null) {
          request.getSession().setAttribute("loginUser", login.getUsername());
          request.getSession().setAttribute("currentUser", login);
          returnPage = "main";
        } else {
          Cookie cookie = new Cookie("message", message);
          cookie.setMaxAge(60);
          response.addCookie(cookie);
        }
      } catch (Exception e) {
        logger.error(e.getCause().getMessage());
        e.printStackTrace();
      }
    }
    return returnPage;
  }

  /**
   * 修改密码
   *
   * @param admin
   * @param response
   * @return
   */
  @RequestMapping("/updatePwd")
  public String getMessage(Admin admin, HttpServletResponse response) {
    JSONObject result = new JSONObject();
    boolean flag = false;
    if (admin.getId() != null) {
      try {
        Admin newAdmin = adminService.get(admin.getId());
        newAdmin.setPassword(admin.getPassword());
        adminService.update(newAdmin);
        flag = true;
      } catch (Exception e) {
        logger.error(e.getCause().getMessage());
        e.printStackTrace();
      }
    }
    result.put("success", flag);
    ResponseUtil.write(response, result);
    return null;
  }

  /**
   * 退出本次登录,清空session属性
   *
   * @param request
   * @return
   */
  @RequestMapping("/logout")
  public String logout(HttpServletRequest request, HttpServletResponse response) {
    JSONObject result = new JSONObject();
    Enumeration em = request.getSession().getAttributeNames();
    while (em.hasMoreElements()) {
      request.getSession().removeAttribute(em.nextElement().toString());
    }
    request.getSession().invalidate();
    result.put("success", true);
    ResponseUtil.write(response, result);
    return null;
  }
}
